Ext.data.SQLProxy = Ext.extend(Ext.data.ClientProxy, {
    
    constructor: function(config) {
    	Ext.data.SQLProxy.superclass.constructor.call(this, config);
        
        this.engine = this.engine || config.engine;

        if (this.engine == undefined) {
            throw "No database reference was provided to the SQLite storage proxy. See Ext.data.SQLiteProxy documentation for details";
        }

        this.initialize();
    },

    //inherit docs
    create: function(operation, callback, scope) {/*
        var records = operation.records,
            length  = records.length,
            ids     = this.getIds(),
            id, record, i;
        
        operation.setStarted();

        for (i = 0; i < length; i++) {
            record = records[i];

            if (record.phantom) {
                record.phantom = false;
                id = this.getNextId();
            } else {
                id = record.getId();
            }

            this.setRecord(record, id);
            ids.push(id);
        }

        this.setIds(ids);

        operation.setCompleted();
        operation.setSuccessful();

        if (typeof callback == 'function') {
            callback.call(scope || this, operation);
        }
                                                  */},

    read: function(operation, callback, scope) {
        
        var records = [],
            me = this,
            model   = this.model,
            tableName = this.tableName || operation.tableName || this.model.modelName,
            fields  = this.tableName ? this.engine.tables[this.tableName].columns : model.prototype.fields.items,
            length  = fields.length,
            i, field, name, sqlSelectList='';
        
        operation.setStarted();
        
        for (i = 0; i < length; i++) {
            field = fields[i];
            name  = field.name;
            sqlSelectList += name + ',';
        }
        
        var sql = 'SELECT ' + sqlSelectList.slice(0,sqlSelectList.length - 1) +
                  '  FROM ' + tableName,
            sqlWhere = '', filters=operation.filters, hostVars =[]
        ;
        
        if (filters)
        for (i = 0; i < filters.length; i++) {
            sqlWhere += filters[i].property + ' = ? ' +  ((i < filters.length-1) ? ',' : '');
            hostVars.push(filters[i].value);
        }
        
        if (operation.id){
            sqlWhere += 'id = ?';
            hostVars.push(operation.id);
        }
        
        if ( sqlWhere.length > 0 ) sql += ' WHERE '+sqlWhere;
        
        var sqlOrderBy = '', sorters = operation.sorters;
        
        if (sorters)
        for (i = 0; i < sorters.length; i++) {
            sqlOrderBy += sorters[i].property +' '+ sorters[i].direction +  ((i < sorters.length-1) ? ',' : '');
        }
        
        if ( sqlOrderBy.length > 0 ) sql += ' ORDER BY '+sqlOrderBy;
        
        console.log(sql);
        
        this.engine.db.transaction(
            function(t){
                t.operation = operation;
                t.proxy = me;
                t.scope = scope;
                t.callback = callback;
                t.executeSql(sql, hostVars, me.dataReady, me.dataError);
            }
        );
        
    },
    
    dataReady: function(t, result){
        var rows = result.rows, records = [], recData = {};
        
        for(var i = 0; i < rows.length; i++) {
            data = new t.proxy.model(rows.item(i));
            records.push(data);
        }
        
        t.operation.resultSet = new Ext.data.ResultSet({
            "records": records,
            total  : records.length,
            loaded : true
        });
        
        t.operation.setSuccessful();
        t.operation.setCompleted();
        
        if (typeof t.callback == "function") {
            t.callback.call(t.scope || t.proxy, t.operation, true);
        }
    },
    
    dataError: function(t, e){
        t.operation.setException(e);
        t.operation.setCompleted();
        
        t.operation.resultSet = new Ext.data.ResultSet({
            "records": [],
            total  : 0,
            loaded : true
        });
        
        if (typeof t.callback == "function") {
            t.callback.call(t.scope || t.proxy, t.operation, false);
        }
    },

    //inherit docs
    update: function(operation, callback, scope) {/*
        var records = operation.records,
            length  = records.length,
            ids     = this.getIds(),
            record, id, i;

        operation.setStarted();

        for (i = 0; i < length; i++) {
            record = records[i];
            this.setRecord(record);
            
            //we need to update the set of ids here because it's possible that a non-phantom record was added
            //to this proxy - in which case the record's id would never have been added via the normal 'create' call
            id = record.getId();
            if (id !== undefined && ids.indexOf(id) == -1) {
                ids.push(id);
            }
        }
        this.setIds(ids);

        operation.setCompleted();
        operation.setSuccessful();

        if (typeof callback == 'function') {
            callback.call(scope || this, operation);
        }
                                                  */},

    //inherit
    destroy: function(operation, callback, scope) {/*
        var records = operation.records,
            length  = records.length,
            ids     = this.getIds(),

            //newIds is a copy of ids, from which we remove the destroyed records
            newIds  = [].concat(ids),
            i;

        for (i = 0; i < length; i++) {
            newIds.remove(records[i].getId());
            this.removeRecord(records[i], false);
        }

        this.setIds(newIds);

        if (typeof callback == 'function') {
            callback.call(scope || this, operation);
        }
                                                   */},

    /**
     * @private
     * 
     */
    initialize: function() {
        return true;
    }

    
});

Ext.data.ProxyMgr.registerType('sql', Ext.data.SQLProxy);